<template>
  <div class="layoutss">
    <div class="header">
      <el-button type="primary" size="mini" @click="handleAdd">{{ $t('btn.save') }}</el-button>
    </div>
    <el-tabs v-model="essential">
      <el-tab-pane :label="$t('market.info')" name="information">
        <el-form ref="purchaseRef" :model="process" label-width="100px">
          <el-form-item :label="$t('market.billNo')">
            <el-input v-model="process.fbillno" size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.supplier')">
            <el-autocomplete
              v-model.trim="process.fsupplier"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch9"
              size="mini"
              @focus="handleSupplier"
              @select="supplierData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="supplierVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-form>
        <div>
          <div style="margin-bottom: 5px">
            <el-button size="mini" @click="twoLine(1)">{{ $t('btn.newHang') }}<!--新增行--></el-button>
            <el-button size="mini" @click="twoLine(2)">{{ $t('btn.delHang') }}<!--删除行--></el-button>
            <el-button size="mini" @click.native="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
          </div>
          <jc-table
            :table-data="tableData"
            :table-header="tableHeader"
            serial
            :table-height="tableHeight"
            @rowClick="rowClick"
            @cellClick="cellClick"
          >
            <!--    工序名称       -->
            <template v-slot:billSlot="col">
              <el-input v-model="col.scope.row.fworkname">
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="sourcing(col.scope.$index)"
                />
              </el-input>
            </template>
            <!--    倍数       -->
            <template v-slot:btnState="col">
              <el-input-number v-model="col.scope.row.fnum" @change="headelMoney(col.scope.$index)" />
            </template>
            <!--    单价       -->
            <template v-slot:btnStates="col">
              <el-input-number v-model="col.scope.row.fprice" @change="headelMoney(col.scope.$index)" />
            </template>
            <!--    备注       -->
            <template v-slot:orderSlot="col">
              <el-input v-model="col.scope.row.fremark" />
            </template>
          </jc-table>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!--供应商-->
    <supplier v-if="supplierVisiblit" @supplier="supplierData" />
    <outSourcing v-if="sourcingVis" @supplier="sourcData" />
  </div>
</template>

<script>
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import {
  insertWorkSupplier
} from '@/api/workshop'

export default {
  name: 'OtherWarehousingAdd',
  components: {
    supplier: () => import('@/views/purchasing/purchase/treePurchasePrice/component/supplier'),
    outSourcing: () => import('@/views/workshop/supplierProcess/supplierProcessAdd/component/outSourcing')
  },
  mixins: [autocomplete],
  data() {
    return {
      essential: 'information',
      supplierVisiblit: false, // 供应商
      sourcingVis: false, // 工序
      fnumber: '',
      // 表头数据
      tableData: [],
      tableHeader: [
        { label: this.$t('market.processname'), prop: 'fnumber', minWidth: '100' },
        { label: this.$t('market.processnumber'), type: 'bill', prop: 'fworkname', minWidth: '100' },
        { label: this.$t('market.multiple'), type: 'state', prop: 'fnum', minWidth: '80' },
        { label: this.$t('market.fprice'), type: 'states', prop: 'fprice', minWidth: '80' },
        { label: this.$t('market.billamount'), prop: 'fsumprice', minWidth: '80' },
        { label: this.$t('market.note'), type: 'order', prop: 'fremark', minWidth: '80' }
      ],
      process: {},
      tableHeight: ''
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 250px)' : '400px'
  },
  methods: {
    // 新增
    handleAdd() {
      const data = {
        ...this.process,
        fdocumentstatus: 'A',
        detail: this.tableData
      }
      insertWorkSupplier(data).then(res => {
        const { code, message } = res
        if (code !== 0) {
          this.$confirm(message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        // 关闭当前页面
        this.$store.dispatch('tagsView/guanbi', this.$route)
        this.$router.push({ path: `/supplierProcessDetail/${res.data}` })
        this.$message({ showClose: true, message: res.message, type: 'success' })
      })
    },
    // 获取供应商
    supplierData(ev) {
      if (ev.fsupplierId) {
        this.process.fsupplierid = ev.fsupplierId
        this.process.fsupplier = ev.fname
      }
      this.supplierVisiblit = false
    },
    // 工序名称
    sourcing(index) {
      this.sourcingVis = true
      this.indexSelf = index
    },
    sourcData(ev) {
      if (ev.fid) {
        this.tableData[this.indexSelf].fworkname = ev.fname
        this.tableData[this.indexSelf].fnumber = ev.fnumber
        this.tableData[this.indexSelf].fworknameid = ev.fid
      }
      this.sourcingVis = false
    },
    // 获取金额
    headelMoney(index) {
      const item = this.tableData[index]
      item.fsumprice = item.fnum * item.fprice
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 新增 删除行
    twoLine(ev) {
      const data = {
        fnumber: '',
        fworkname: '',
        fnum: 1,
        fprice: 0,
        fsumprice: 0,
        fremark: ''
      }
      if (ev === 1) {
        this.tableData.push(data)
      } else {
        const fid = this.tableData[this.indexSelf].fentryId
        this.delArr.push(fid)
        this.tableData.splice(this.indexSelf, 1)
      }
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      this.property = column.property
      this.subscript = row.index
    },
    // 批量填充
    batchFill() {
      const item = this.tableData[this.subscript][this.property]
      const ckid = this.tableData[this.subscript].fstockId
      const sumQty = this.tableData[this.subscript].stockQty
      this.tableData.map((res, index) => {
        if (index >= this.subscript) {
          if (this.property === 'fstockName') {
            res.fstockId = ckid
            res.stockQty = sumQty
          }
          res[this.property] = item
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.layoutss {
  @include listBom;
  padding: 10px;
  background: #FFFFFF;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-input__icon {
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      //height: 35px;
      width: 275px;
      margin-bottom: 0;
      .btnType{
        margin-right: 20px;
      }
      .el-date-editor.el-input{
        width: 100% !important;
      }
      .el-select{
        width: 100%;
      }
    }
  }
}
</style>
